package com.example.springtransaction.service;

import com.example.springtransaction.mapper.LogMapper;
import com.example.springtransaction.model.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-03-17
 * Time: 23:43
 */
@Service
public class LogService {
    @Autowired
    private LogMapper logMapper;

//    @Transactional
//    @Transactional(propagation = Propagation.NOT_SUPPORTED)
//    @Transactional(propagation = Propagation.REQUIRES_NEW)
//    @Transactional(propagation = Propagation.NESTED)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int add(LogInfo logInfo){
        int num=10/0;
        return logMapper.add(logInfo);
    }

    @Transactional(propagation = Propagation.NESTED)
    public int save(LogInfo logInfo){
        int result=logMapper.add(logInfo);
//        注意这里的异常必须捕获并手动设置回滚,
//        不然异常会往上抛, 只要涉及这个异常并没有处理的都会回滚
        try {
            int num=10/0;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }
}
